package jssdk

import (
	"fmt"
	"wxsdk/miniapp/core"
	"encoding/json"
	"github.com/astaxie/beego"
)

type JsapiTicket struct {
	Ticket    string `json:"ticket"`
	ExpiresIn int64  `json:"expires_in"`
}

func UpdateTicket(accessToken string) (ticket *JsapiTicket, err error) {
	var body []byte
	var incompleteURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=%s"
	urlStr := fmt.Sprintf(incompleteURL, accessToken)
	body, err = core.GetCommonFileByHttp(urlStr)
	if err != nil {
		beego.Debug("GetCommonFileByHttp", err.Error())
		return
	}
	var result struct {
		core.Error
		JsapiTicket
	}
	err = json.Unmarshal(body, &result)
	if err != nil {
		beego.Debug(err.Error())
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	ticket = &result.JsapiTicket
	return
}

